<template>
  <div v-loading.fullscreen="loading">
    <el-dialog :visible.sync="isShow" title="分配角色" @close="closeEvent">
      <el-checkbox-group v-model="checkArr">
        <!-- 
              label:是选中后的值
              v-model的值如果是数组，点击后增加与删除相应的值
                             非数组，点击后它的值会是false与true的切换
           -->
        <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{
          item.name
        }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { sysRole } from '@/api/setting.js'
import { sysUser } from '@/api/user'
import { sysUserAssignRoles } from '@/api/employees.js'
export default {
  data () {
    return {
      loading: false,
      isShow: false,
      list: [],
      checkArr: [],
      id: '' // 当前点击项员工id
    }
  },
  created () {
    this.getData()
  },
  methods: {
    //   关闭事件
    closeEvent () {
      this.isShow = false
      //   关闭时清空选择
      this.checkArr = []
    },
    // 获取角色列表
    async getData () {
      const res = await sysRole({ pagesize: 300 })
      this.list = res.data.rows
      console.log('角色 列表', res)
    },
    // 获取当前员工基本信息包含角色
    async getUserInfo (id) {
      this.loading = true
      this.id = id
      const res = await sysUser(id)
      //   Array.isArray(res.data.roleIds)?
      this.checkArr = res.data.roleIds || []
      this.loading = false
      console.log('当前点击项员工详情', res)
    },
    // 提交
    async submit () {
      await sysUserAssignRoles(this.id, this.checkArr)
      this.$message.success('分配角色成功')
      this.closeEvent()
    }
  }
}
</script>
<style></style>
